var that;
const app = getApp();
var carrierArray = [];
Page({
  /**组件的初始数据*/
  data: {
    mobile: "",
    name: "",
    cardNum: "",
    userType: "",
    carrierName: "",
    carrierId: "",
    carName: "",
    phone: "",
    countries: ["请选择", "AAAAA", "AAAA", "AAA", "AA", "A"],
    countryIndex: 0,
    countries2: ["请选择", "预付扣款", "开增票", "油气卡", "全现金"],
    countryIndex2: 0,
    carriers: [],
    carrierIndex: 0,
  },
  onLoad: function () {
    that = this;
    wx.setNavigationBarTitle({
      title: '资料修改'
    });
    getCarriers();
  },
  bindCountryChange: function (e) {
    this.setData({
      countryIndex: e.detail.value,
      transLevel: that.data.countries[e.detail.value]
    })
  },
  bindCountryChange2: function (e) {
    this.setData({
      countryIndex2: e.detail.value,
      financeType: that.data.countries2[e.detail.value]
    })
  },


  bindCarrierChange: function (e) {
    var carrierId = "";
    var financeType = "";
    var accountNum = "";
    var accountName = "";
    var transLevel = "";
    for (const i in carrierArray) {
      if (carrierArray[i].name == that.data.carriers[e.detail.value]) {
        carrierId = carrierArray[i].id;
        financeType = carrierArray[i].financeType;
        accountNum = carrierArray[i].accountNum;
        accountName = carrierArray[i].accountName;
        transLevel = carrierArray[i].transLevel;
        break;
      }
    }
    this.setData({
      carrierIndex: e.detail.value,
      carrierId: carrierId,
      carrierName: that.data.carriers[e.detail.value],

      accountName: accountName,
      accountNum: accountNum,
      transLevel: transLevel,
      financeType: financeType,
      countryIndex: that.data.countries.indexOf(transLevel),
      countryIndex2: that.data.countries2.indexOf(financeType)
    })
  },
  //监听输入的手机号
  mobileInp: function (e) {
    this.data.driverMobile = e.detail.value;
  },
  nameInp: function (e) {
    this.data.driverName = e.detail.value;
  },
  cardNumInp: function (e) {
    this.data.driverNum = e.detail.value;
  },
  carNameInp: function (e) {
    this.data.carNum = e.detail.value;
  },

  accountNameInp: function (e) {
    this.data.accountName = e.detail.value;
  },
  accountNumInp: function (e) {
    this.data.accountNum = e.detail.value;
  },


  phoneInp: function (e) {
    this.data.phone = e.detail.value;
  },

  btnclick: function () {
    verification();
    if (verification()) {
      wx.request({
        url: app.serverIP + '/kkl/cms/app/CarDriver/edit?token=' + app.token, //请求地址
        data: JSON.stringify({
          "id": app.id,
          "openId": app.openId,
          "companyId": app.companyId,
          "driverMobile": that.data.driverMobile,
          "driverName": that.data.driverName,
          "driverNum": that.data.driverNum,
          "transLevel": that.data.transLevel,
          "financeType": that.data.financeType,
          "accountName": that.data.accountName,
          "accountNum": that.data.accountNum,
          "carNum": that.data.carNum,
          "carrierName": that.data.carrierName,
          "carrierId": that.data.carrierId,
        }),
        // "driverState": 1,
        method: 'POST',
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 1500
            })
            getApp().userInfo = {
              "id": app.id,
              "openId": app.openId,
              "companyId": app.companyId,

              "driverMobile": that.data.driverMobile,
              "driverName": that.data.driverName,
              "driverNum": that.data.driverNum,
              "transLevel": that.data.transLevel,
              "financeType": that.data.financeType,
              "accountName": that.data.accountName,
              "accountNum": that.data.accountNum,
              "carNum": that.data.carNum,
              "carrierName": that.data.carrierName,
              "carrierId": that.data.carrierId,
            }
          } else {
            wx.showToast({
              title: '保存失败' + res.data.message,
              icon: 'none',
              duration: 1500
            })
          }
        },
        fail: function (err) {
          console.log(err);
        }
      })
    }
  },
})

function verification() {
  if (that.data.driverMobile.length == 0) {
    showToast('手机号不能为空', 'none', 1500);
    return false;
  }
  if (that.data.driverMobile.length != 11) {
    showToast('请输入正确的手机号', 'none', 1500);
    return false;
  }
  if (that.data.driverName.length == 0) {
    showToast('姓名不能为空', 'none', 1500);
    return false;
  }
  if (that.data.carrierId == "" || that.data.carrierId == "请选择") {
    showToast('请选择运力类别', 'none', 1500);
    return false;
  }
  return true;
}
//先加载承运商基础数据,在读取需要修改的数据
function getCarriers() {
  wx.request({
    url: app.serverIP + '/kkl/cms/app/Carrier/list?token=' + app.token,
    dataType: "json",
    success: function (res) {
      if (res.data.code == "200") {
        carrierArray = res.data.data;
        var carrierData = ["请选择"];
        for (const i in res.data.data) {
          carrierData.push(res.data.data[i].name);
        }
        that.setData({
          carriers: carrierData
        })
      }
      // 加载完毕承运商基础数据,在读取需要修改的数据
      getData();
    },
  })
}

function showToast(title, icon, duration) {
  wx.showToast({
    title: title,
    icon: icon,
    duration: duration
  })
}

function getData() {
  wx.request({
    url: app.serverIP + '/kkl/cms/app/CarDriver/' + getApp().id + '?token=' + app.token,
    dataType: "json",
    success: function (res) {
      if (res.data.code == "200") {
        if (res.data.data.transLevel == "" || res.data.data.transLevel == "-") {
          res.data.data.transLevel = "请选择"
        }
        if (res.data.data.financeType == "" || res.data.data.financeType == "-") {
          res.data.data.financeType = "请选择"
        }
        if (res.data.data.carrierName == "" || res.data.data.carrierName == "-") {
          res.data.data.carrierName = "请选择"
        }
        if (res.data.data.carNum == "-") {
          res.data.data.carNum = ""
        }
        that.setData({

          countryIndex: that.data.countries.indexOf(res.data.data.transLevel),
          countryIndex2: that.data.countries2.indexOf(res.data.data.financeType),
          carrierIndex: that.data.carriers.indexOf(res.data.data.carrierName),

          driverMobile: res.data.data.driverMobile,
          driverName: res.data.data.driverName,
          driverNum: res.data.data.driverNum,

          transLevel: res.data.data.transLevel,
          financeType: res.data.data.financeType,
          accountName: res.data.data.accountName,
          accountNum: res.data.data.accountNum,

          carNum: res.data.data.carNum,
          carrierName: res.data.data.carrierName,
          carrierId: res.data.data.carrierId,
        })
      }
    },
  })
}